//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 6
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace CreateContactWithEmailAddresses
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            CreateContactWithEmailAddresses(binding);
        }

        /// <summary>
        /// Create a contact with email addresses (Chapter 6)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <returns>ItemId of new contact</returns>
        /// 
        public static ItemIdType CreateContactWithEmailAddresses(
                                ExchangeServiceBinding binding)
        {
            CreateItemType request = new CreateItemType();
            request.Items = new NonEmptyArrayOfAllItemsType();

            ContactItemType newContact = new ContactItemType();
            // Set all the appropriate name fields
            //
            newContact.GivenName = "Jane";
            newContact.Surname = "Dow";
            newContact.FileAsMapping = FileAsMappingType.FirstSpaceLast;
            newContact.FileAsMappingSpecified = true;
            newContact.DisplayName = "Jane Dow";
            newContact.Subject = "Jane Dow";

            // Set up our single email address.
            //
            EmailAddressDictionaryEntryType address = new
                     EmailAddressDictionaryEntryType();
            address.Key = EmailAddressKeyType.EmailAddress1;
            address.Value = "jane.dow@contoso.com";
            newContact.EmailAddresses = new EmailAddressDictionaryEntryType[] { 
           address };

            // Now let's also set the email address display name and routing type
            //
            PathToExtendedFieldType emailDisplayPath =
                   PathToExtendedFieldType.BuildGuidId(
                         DistinguishedPropertySetType.Address,
                         32900,
                         MapiPropertyTypeType.String);
            PathToExtendedFieldType emailRoutingPath =
                   PathToExtendedFieldType.BuildGuidId(
                         DistinguishedPropertySetType.Address,
                         32898,
                         MapiPropertyTypeType.String);
            ExtendedPropertyType emailDisplayProp = new ExtendedPropertyType(
                         emailDisplayPath,
                         "Janie >> jane.dow@contoso.com");
            ExtendedPropertyType emailRoutingProp = new ExtendedPropertyType(
                         emailRoutingPath,
                         "SMTP");

            newContact.ExtendedProperty = new ExtendedPropertyType[] { 
               emailDisplayProp, emailRoutingProp };

            request.Items.Items = new ItemType[] { newContact };

            // Now make the call
            //
            ItemInfoResponseMessageType responseMessage =
                   binding.CreateItem(request).ResponseMessages.Items[0] as
                        ItemInfoResponseMessageType;
            return responseMessage.Items.Items[0].ItemId;
        }

    }
}
